package com.only4play.starfish.common;

import lombok.Getter;
import lombok.Setter;

/**
 * 统一返回数据实体
 *
 * @param <T>
 */
@Getter
@Setter
public class GenericResponse<T> extends AbstractResponse {
    //请求状态
    private boolean success;

    //返回成功的时候，返回的内容
    private T payload;

    public GenericResponse(Integer errorCode, String errorMsg, boolean success, T payload) {
        super(errorCode, errorMsg);
        this.payload = payload;
        this.success = success;
    }

    public static <T> GenericResponse<T> errorMsg(RespCodeEnum code, String message) {
        return of(code.getErrorCode(), message, false, null);
    }

    public static <T> GenericResponse<T> error(RespCodeEnum code) {
        return of(code.getErrorCode(), code.getErrorMessage(), false, null);
    }

    public static <T> GenericResponse<T> success(T payload) {
        return of(RespCodeEnum.SUCCESS.getErrorCode(), RespCodeEnum.SUCCESS.getErrorMessage(), true, payload);
    }

    protected static <T> GenericResponse<T> of(int errorCode, String errorMsg, boolean success, T payload) {
        return new GenericResponse<>(errorCode, errorMsg, success, payload);
    }

}


